﻿using System;
using System.Collections.Generic;
using Basic.Model.Entity;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;

namespace Basic.Model.EntityManager
{
    public class RoleGroupManager
    {
        public static void Add(ISession session, RoleGroup an)
        {
            session.Save(an);
        }

        public static void Delete(ISession session, RoleGroup an)
        {
            session.Delete(an);
        }

        public static void Update(ISession session, RoleGroup an)
        {
            session.Update(an);
        }

        public static RoleGroup Get(ISession session, int itemId)
        {
            return session.Get<RoleGroup>(itemId);
        }

        public static RoleGroup GetByCondition(ISession session, int roleGroupId, string roleGroupName, bool isRoles)
        {
            ICriteria c = session.CreateCriteria(typeof(RoleGroup));
            if (roleGroupId >= 0)
                c.Add(Expression.Eq("roleGroupId", roleGroupId));
            if (!String.IsNullOrEmpty(roleGroupName))
                c.Add(Expression.Eq("roleGroupName", roleGroupName));
            if (isRoles)
            {
                c.SetFetchMode("roles", FetchMode.Eager);
                c.SetResultTransformer(NHibernate.CriteriaUtil.DistinctRootEntity);
            }
            return c.UniqueResult<RoleGroup>();
        }

        public static IList<RoleGroup> GetAll(ISession session)
        {
            ICriteria c = session.CreateCriteria(typeof(RoleGroup));
            return c.List<RoleGroup>();
        }

        public static IList<RoleGroup> GetAllByCondition(ISession session, bool isRoles, int rowIndex, int rowCount)
        {
            ICriteria c = session.CreateCriteria(typeof(RoleGroup));
            if (rowIndex >= 0)
                c.SetFirstResult(rowIndex);
            if (rowCount >= 0)
                c.SetMaxResults(rowCount);
            if (isRoles)
            {
                c.SetFetchMode("roles", FetchMode.Eager);
                c.SetResultTransformer(NHibernate.CriteriaUtil.DistinctRootEntity);
            }
            return c.List<RoleGroup>();
        }
        public static int GetAllByConditionCount(ISession session)
        {
            ICriteria c = session.CreateCriteria(typeof(RoleGroup));
            return (int)c.SetProjection(Projections.Count("roleGroupId")).UniqueResult();
        }
    }
}
